package de.fhd.pms.dao;


/** 
 * A Test class (Junit) for {@link ActionDao} to find out if all Methods defined in ActionDao are working fine.
 * This class uses a special test-database "hitchtest".
 */
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import de.fhd.pms.model.Action;
import de.fhd.pms.model.User;

public class ActionDaoTest  extends AbstractDataAccessTest {

	private static final Log log = LogFactory.getLog(UserDaoTest.class);

	private static final String[] configFiles = new String[] { "test-application-context.xml" };

	private ApplicationContext ctx = null;
	
	Action action = null;
	User user = null; 
	private Date birthdate;
	
	private String tableName = "action";
	private String[] tables = { tableName };
	
	ActionDao actionDao = null;
	UserDao userDao = null;
	
	/**Set ActionDao
	 @param actionDao
	  */
	
	public void setActionDao(ActionDao actionDao){
		this.actionDao = actionDao;
	};
	
	/**Set UserDao
	 @param userDao
	  */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	/**Setup Configuration
	  */
	@Before
	public void onSetUp() {
		
		
		user = new User();

		Date date = new Date();

		birthdate = date;

		user.setEnabled(1);
		user.setBirthdate(birthdate);
		user.setGender(1);
		user.setEmail("test@test.lv");
		user.setUsername("andreas");
		user.setPassword("qwER12!@");
		user.setPasswordConfirm("qwER12!@");
		user.setName("Andreas");
		user.setLastname("Janow");
		
		action = new Action();
		action.setDescription("Some small description");
		action.setEnd_date(date);
		action.setEnd_hours(11);
		action.setEnd_minutes(10);
		action.setName("Action name");
		action.setStart_date(date);
		action.setStart_hours(10);
		action.setStart_minutes(10);
		action.setUser(user);
		
		ctx = new ClassPathXmlApplicationContext(configFiles);
		log.debug("ctx: " + ctx);
	}
	/**
	 * A Test to find out if the {@link Action} is created by the {@link User}.
	 */
	@Test
	public void testFindByActionId1(){
		
		userDao.save(user);
		actionDao.save(action);
		
		Integer id = -1;
		
		List<Action> alle = actionDao.findAll();
		if (alle == null) {
			id = -1;
		} 
		for (Action a : alle) {
			if ( a.getName().equals( action.getName() ) ){
				id = a.getId();
				break;
			} else {
				id = -1; 
			}
		}
		
		System.out.println(actionDao);
		System.out.println(id);
		
		Action testAction = actionDao.findByActionId(id);
		
		System.out.println(action);
		System.out.println(testAction);

		

		userDao.delete(user);
		actionDao.delete(action);
		assertEquals(action.getId(), testAction.getId());
		
	}
	/**
	 * A Test to find out if a {@link Action} can be created.
	 */
	@Test
	public void testFindByActionId2(){
		Action testAction = actionDao.findByActionId(2);
		assertEquals(null, testAction);
	}
	
	/**
	 * A Test to find all Actions from the test-database.
	 */
	@Test
	public void testFindAll(){
		
		userDao.save(user);
		
		actionDao.save(action);
		action.setId(null);
		actionDao.save(action);
		
		List<Action> alle = actionDao.findAll();
		assertEquals(alle.size(), 2);
		
		for (Action a : alle) {
			actionDao.delete(a);
		}
		
		userDao.delete(user);
	}
	
	
	
	
}
